package com.sharainy.contoller;

import com.alibaba.fastjson.JSONObject;
import com.sharainy.pojo.Merchant;
import com.sharainy.pojo.User;
import com.sharainy.service.MerchantService;
import com.sharainy.service.MeterService;
import com.sharainy.util.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.Instant;
import java.util.List;

@Controller
@RequestMapping("/merchant")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private MeterService meterService;

    @RequestMapping(value = "/merchantPage")
    public String index(){
        return "merchant";
    }
    @RequestMapping("/getMerchantList")
    @ResponseBody
    public RestResponse getMerchantList(HttpServletRequest request){
        JSONObject jsonMain =new JSONObject();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<Merchant> merchantList = merchantService.selectAllMerchant(user.getCompanyid() + "");
        RestResponse restResponse = RestResponse.succuess();
        jsonMain.put("merchantList",merchantList);
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("/addMerchant")
    @ResponseBody
    public RestResponse addMerchant(Merchant merchant,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        merchant.setCompanyid(user.getCompanyid());
        merchant.setPwd("hzdbb123");

        merchant.setAccesstime(Instant.now().getEpochSecond());
        String[] meterArr = merchant.getMeters().split(",");
        try{
            if (meterArr.length>0){
                for (int i=0;i<meterArr.length;i++) {
                    meterService.setMeterStateToU(meterArr[i]);
                }
            }
            merchantService.addMerchant(merchant);
            int iid = merchant.getId();
            return RestResponse.succuess();
        }catch (Exception e){
            e.printStackTrace();
            return RestResponse.fail();
        }
    }
    @RequestMapping("/oppenMerchant")
    @ResponseBody
    public RestResponse oppenMerchant(String id){
        String[] ids = {id};
        merchantService.updateStates(ids);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
    @RequestMapping("/pinHouseholds")
    @ResponseBody
    public RestResponse pinHouseholds(String id){
        merchantService.pinHouseholds(id);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }

    @RequestMapping("/modifyMerchant")
    @ResponseBody
    public RestResponse modifyMerchant(Merchant merchant,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        /*String id = request.getParameter("id");*/
        merchant.setCompanyid(user.getCompanyid());
        merchant.setAccesstime(Instant.now().getEpochSecond());

        try{
            merchantService.updateMerchant(merchant);
            return RestResponse.succuess();
        }catch (Exception e){
            e.printStackTrace();
            return RestResponse.fail();
        }
    }
}
